package com.ww.core.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ww.core.R;
import com.ww.core.util.CoreSharedPreferencesHelper;

public class RememberDialog {
	private Context context;
	private Dialog dlg;

	public RememberDialog(Context context) {
		this.context = context;
	}

	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 */
	public void show(String title, String content, OnClickListener okClick,final String key) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_remember);
		TextView titleView = (TextView) window.findViewById(R.id.title);
		titleView.setText(title);
		TextView contentView = (TextView) window.findViewById(R.id.content);
		contentView.setText(content);
		Button ok = (Button) window.findViewById(R.id.ok);
		ok.setOnClickListener(okClick);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dlg.cancel();
			}
		});
		
		CheckBox check = (CheckBox) window.findViewById(R.id.check);
		check.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
					h.setValue(key, isChecked+"");
			}});
	}
	
	
	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 */
	public void show(String title, String content,int icon, OnClickListener okClick,final String key) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_remember);
		TextView titleView = (TextView) window.findViewById(R.id.title);
		titleView.setText(title);
		TextView contentView = (TextView) window.findViewById(R.id.content);
		ImageView imageView = (ImageView) window.findViewById(R.id.icon);
		CheckBox check = (CheckBox) window.findViewById(R.id.check);
		imageView.setImageResource(icon);
		imageView.setVisibility(View.VISIBLE);
		contentView.setText(content);
		Button ok = (Button) window.findViewById(R.id.ok);
		ok.setOnClickListener(okClick);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dlg.cancel();
			}
		});
		
		check.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
					h.setValue(key, isChecked+"");
			}});
		
	}
	
	


	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 * @param cancelClick 取消按钮点击事件.
	 */
	public void show(String title, String content, OnClickListener okClick, OnClickListener cancelClick,final String key) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_remember);
		TextView titleView = (TextView) window.findViewById(R.id.title);
		titleView.setText(title);
		TextView contentView = (TextView) window.findViewById(R.id.content);
		contentView.setText(content);
		Button ok = (Button) window.findViewById(R.id.ok);
		ok.setOnClickListener(okClick);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		cancel.setOnClickListener(cancelClick);
		
		CheckBox check = (CheckBox) window.findViewById(R.id.check);
		check.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				CoreSharedPreferencesHelper h = new CoreSharedPreferencesHelper(context);
					h.setValue(key, isChecked+"");
			}});
	}

	public void cancel() {
		if (dlg != null) {
			dlg.cancel();
		}
	}
}
